<?php

class Usuario_model extends Model {
	var $table="cliente";
	var $tableweb="clientes";
	var $table_pedido="pedido";
  var $table_estado_civil="estado_civil";
  var $table_sexo= "sexo";
    	
	var $campos = array(
	array(
			"type" => "text",
			"name" => "apelido",
			"id"=>"apelido",
			"title" => "Gostaria de ser chamado de",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"20",
		),
		array(
			"type" => "text",
			"name" => "nome",
			"id"=>"nome",
			"title" => "Nome",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"50",
		),		
		array(
			"type" => "text",
			"name" => "cpf",
			"id"=>"cpf",
			"title" => "CPF",
			"class" => "vObrigatorio vCPF",
		),
		array(
			"type" => "text",
			"name" => "rg",
			"id"=>"rg",
			"title" => "RG",
			"class" => "vObrigatorio",
		),
			array(
			"type" => "text",
			"name" => "orgao_expedidor",
			"id"=>"orgao_expedidor",
			"title" => "Orgão Expedidor",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"5",
		),
		array(
		"type" => "select",
			"name" => "uf_rg",
			"id"=>"uf_rg",
			"title" => "Estado do RG",
			"class" => "vObrigatorio",
			"options"=> array('' => '..', 'AC'  => 'Acre', 'AL'  => 'Alagoas','AP'  => 'Amapá', 
			                  'AM'  => 'Amazonas','BA'  => 'Bahia','CE'  => 'Ceará','DF'  => 'Distrito Federal',
                        'ES'  => 'Espírito Santo','GO'  => 'Goiás','MA'  => 'Maranhão',
                        'MT'  => 'Mato Grosso','MS'  => 'Mato Grosso do Sul','MG'  => 'Minas Gerais','PA'  => 'Pará',
                        'PB'  => 'Paraíba','PR'  => 'Paraná','PE'  => 'Pernambuco',
                        'PI'  => 'Piauí','RJ'  => 'Rio de Janeiro','RN'  => 'Rio Grande do Norte',
                        'RS'  => 'Rio Grande do Sul','RO'  => 'Rondônia','RR'  => 'Roraima',
                        'SC'  => 'Santa Catarina','SP'  => 'São Paulo','SE'  => 'Sergipe','TO'  => 'Tocantins'),
			"selected" => 'SP',
		),
		array(
			"type" => "text",
			"name" => "dt_nasc",
			"id"=>"dt_nasc",
			"title" => "Data de Nascimento",
			"class" => "vObrigatorio data",
		),
		array(
		  "type" => "select",
			"name" => "sexo",
			"id" => "sexo",
			"title" => "Sexo",
			"class" => "vObrigatorio",
			"options"=> array(''=>"..",1=>"Feminino", 2=>"Masculino"),
			"selected"=>"0",
		),
		array(
			"type" => "text",
			"name" => "nacionalidade",
			"id"=>"nacionalidade",
			"title" => "Nacionalidade",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"20",
		),
  array(
		  "type" => "select",
			"name" => "estado_civil",
			"id" => "estado_civil",
			"title" => "Estado Civil",
			"class" => "vObrigatorio",
			"options"=> array(''=>"..",1=>"Solteiro(a)", 2=>"Casado(a)",3=>"Divorciado(a)",4=>"Viuvo(a)",5=>"Outros"),
			"selected"=>"0",
		),
		array(
			"type" => "text",
			"name" => "profissao",
			"id"=>"profissao",
			"title" => "Profissão",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"30",
		),
		array(
			"type" => "text",
			"name" => "email",
			"id"=>"email",
			"title" => "E-mail",
			"class" => "vObrigatorio vEmail",
			"size"=>"30",
			"maxlength"=>"50",
		),
		array(
			"type" => "password",
			"name" => "senha",
			"id"=>"email",
			"title" => "Senha",
			"class" => "vObrigatorio vConfirma",
			"size"=>"30",
			"maxlength"=>"8",
		),
		array(
			"type" => "password",
			"name" => "senha2",
			"id"=>"senha2",
			"title" => "Repita a Senha",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"8",
		),
		array(
			"type" => "text",
			"name" => "endereco",
			"id"=>"endereco",
			"title" => "Endereço",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"50",
		),
			array(
			"type" => "text",
			"name" => "numero",
			"id"=>"numero",
			"title" => "Número",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"5",
		),
			array(
			"type" => "text",
			"name" => "complemento",
			"id"=>"complemento",
			"title" => "Complemento",
			"class" => "complemento",
			"size"=>"30",
			"maxlength"=>"20",
		),
			array(
			"type" => "text",
			"name" => "bairro",
			"id"=>"bairro",
			"title" => "Bairro",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"20",
		),
			array(
			"type" => "text",
			"name" => "cep",
			"id"=>"cep",
			"title" => "CEP",
			"class" => "vObrigatorio cep",
		),
			array(
			"type" => "text",
			"name" => "cidade",
			"id"=>"cidade",
			"title" => "Cidade",
			"class" => "vObrigatorio",
			"size"=>"30",
			"maxlength"=>"20",
		),
		array(
		"type" => "select",
			"name" => "estado",
			"id"=>"estado",
			"title" => "Estado",
			"class" => "vObrigatorio",
			"options"=> array('' => '..', 'AC'  => 'Acre', 'AL'  => 'Alagoas','AP'  => 'Amapá', 
			                  'AM'  => 'Amazonas','BA'  => 'Bahia','CE'  => 'Ceará','DF'  => 'Distrito Federal',
                        'ES'  => 'Espírito Santo','GO'  => 'Goiás','MA'  => 'Maranhão',
                        'MT'  => 'Mato Grosso','MS'  => 'Mato Grosso do Sul','MG'  => 'Minas Gerais','PA'  => 'Pará',
                        'PB'  => 'Paraíba','PR'  => 'Paraná','PE'  => 'Pernambuco',
                        'PI'  => 'Piauí','RJ'  => 'Rio de Janeiro','RN'  => 'Rio Grande do Norte',
                        'RS'  => 'Rio Grande do Sul','RO'  => 'Rondônia','RR'  => 'Roraima',
                        'SC'  => 'Santa Catarina','SP'  => 'São Paulo','SE'  => 'Sergipe','TO'  => 'Tocantins'),
			"selected" => 'SP',
		),
	array(
			"type" => "text",
			"name" => "telefone",
			"id"=>"telefone",
			"title" => "Telefone",
			"class" => "vObrigatorio telefone",
		),
	array(
			"type" => "text",
			"name" => "celular",
			"id"=>"celular",
			"title" => "Celular",
			"class" => "vObrigatorio telefone",
		),	
array(
			"type" => "text",
			"name" => "endereco_comercial",
			"id"=>"endereco_comercial",
			"title" => "Endereço Comercial",
			"class" => "",
			"size"=>"30",
			"maxlength"=>"50",
		),
			array(
			"type" => "text",
			"name" => "numero_comercial",
			"id"=>"numero_comercial",
			"title" => "Número Comercial",
			"class" => "",
			"size"=>"30",
			"maxlength"=>"5",
		),
			array(
			"type" => "text",
			"name" => "complemento_comercial",
			"id"=>"complemento_comercial",
			"title" => "Complemento Comercial",
			"class" => "",
			"size"=>"30",
			"maxlength"=>"20",
		),
			array(
			"type" => "text",
			"name" => "bairro_comercial",
			"id"=>"bairro_comercial",
			"title" => "Bairro Comercial",
			"class" => "",
			"size"=>"30",
			"maxlength"=>"20",
		),
			array(
			"type" => "text",
			"name" => "cep_comercial",
			"id"=>"cep_comercial",
			"title" => "CEP Comercial",
			"class" => "cep",
		),
			array(
			"type" => "text",
			"name" => "cidade_comercial",
			"id"=>"cidade_comercial",
			"title" => "Cidade Comercial",
			"class" => "",
			"size"=>"30",
			"maxlength"=>"20",
		),
		array(
		"type" => "select",
			"name" => "estado_comercial",
			"id"=>"estado_comercial",
			"title" => "Estado Comercial",
			"class" => "",
			"options"=> array('0' => '..', 'AC'  => 'Acre', 'AL'  => 'Alagoas','AP'  => 'Amapá', 
			                  'AM'  => 'Amazonas','BA'  => 'Bahia','CE'  => 'Ceará','DF'  => 'Distrito Federal',
                        'ES'  => 'Espírito Santo','GO'  => 'Goiás','MA'  => 'Maranhão',
                        'MT'  => 'Mato Grosso','MS'  => 'Mato Grosso do Sul','MG'  => 'Minas Gerais','PA'  => 'Pará',
                        'PB'  => 'Paraíba','PR'  => 'Paraná','PE'  => 'Pernambuco',
                        'PI'  => 'Piauí','RJ'  => 'Rio de Janeiro','RN'  => 'Rio Grande do Norte',
                        'RS'  => 'Rio Grande do Sul','RO'  => 'Rondônia','RR'  => 'Roraima',
                        'SC'  => 'Santa Catarina','SP'  => 'São Paulo','SE'  => 'Sergipe','TO'  => 'Tocantins'),
			"selected" => 'SP',
		),
	array(
			"type" => "text",
			"name" => "telefone_comercial",
			"id"=>"telefone_comercial",
			"title" => "Telefone Comercial",
			"class" => "telefone",
		),		
		
		
		
						
	);  

	function __construct(){
		parent::Model();	
		$this->mysql = $this->load->database('default', TRUE);
		//SQL SERVER
		$this->mssql =  $this->load->database('mssql', TRUE);  
	}

  function cadastro_verif($email){
	  $query = $this->db->get_where($this->table, array('email' => $email));
    return $query->result();
	}	

  function login_verifcpf($cpf){
    //SQL SERVER
	  $query = $this->mssql->get_where($this->tableweb, array('Cli_cpfcnpj' => $cpf));
	  //$query = $this->db->get_where($this->tableweb, array('Cli_cpfcnpj' => $cpf));
    return $query->result();
	}
	
	function login_verif($email,$senha){
	  $query = $this->db->get_where($this->table, array('email' => $email,'senha' => $senha));
    return $query->result();
	}
	
	function dados_cliente($id){
	  $query = $this->db->get_where($this->table, array('cliente_id' => $id));
    return $query->result();
	}
	
	function dados_clientecpf($cpf){
	  $query = $this->db->get_where($this->table, array('cpf' => $cpf));
    return $query->result();
	}
	
	function estado_civil($id_estado_civil){
	  $query = $this->db->get_where($this->table_estado_civil, array('id' => $id_estado_civil));
    return $query->result();
	}
	
	function sexo($id_sexo){
	  $query = $this->db->get_where($this->table_sexo, array('id' => $id_sexo));
    return $query->result();
	}
	
	function verifica_compra($id){
	  $query = $this->db->get_where($this->table_pedido, array('id_cliente' => $id, 'status'=> '4'));
    return $query->result();
	}
	
	function verifica_compraweb($cpf){
	  //SQL SERVER
	  $query =  $this->mssql->get_where($this->tableweb, array('Cli_cpfcnpj' => $cpf));
	  //$query = $this->db->get_where($this->tableweb, array('Cli_cpfcnpj' => $cpf));
    return $query->result();
	}
	
	function dados_pedido($id){ 
	  $query = $this->db->order_by("id", "asc");
	  $query = $this->db->get_where($this->table_pedido, array('id_cliente' => $id));
    return $query->result();
	}
	
	function cadastrar($dados){	
  	foreach($dados as $key => $value){
    	if($key=="dt_nasc")
    	  $val[$key] = formatar_data($value);
    	else
    	  $val[$key] = $value;
  	}

		$this->db->insert($this->table, $val);
	}
	
  function info($id){
    $query = $this->db->get_where($this->table, array('cliente_id' => $id));
    return $query->result();
  }	
	
	
	function alterar($id,$dados){	
  	foreach($dados as $key => $value){
    	if($key=="dt_nasc"){
    	  $dia = substr($value,0,2);
        $mes = substr($value,3,2);
        $ano = substr($value,6,4);
              
        $dt_nasc_new = date("Y-m-d", mktime(0,0,0,$mes,$dia,$ano));
        $val[$key] =$dt_nasc_new;
    	}
    	else{
    	  $val[$key] = $value;
    	}
  	}
  	  	
	  $this->db->where('cliente_id', $id);
    $this->db->update($this->table, $val); 
	}
	
	function consulta_usu(){
    $this->db->select_max('cliente_id','cliente_id');
    $query = $this->db->get($this->table);
    return $query->result();
  }
	
	function unico($campo, $valor)
	{
     $this->db->where($campo, $valor);
	   $total= $this->db->count_all_results($this->table);
	   if($total)
	     return FALSE;
	   else
	     return TRUE;
      
  }
	
}
?>